package com.woniuxy.dr_mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.dr_mall.entity.*;
import com.woniuxy.dr_mall.exception.AccountNotExistException;
import com.woniuxy.dr_mall.exception.PasswordErrorException;
import com.woniuxy.dr_mall.exception.ServiceException;
import com.woniuxy.dr_mall.global.ResponseStateCode;
import com.woniuxy.dr_mall.service.IDrManagerMenuService;
import com.woniuxy.dr_mall.service.IDrManagerPermService;
import com.woniuxy.dr_mall.service.IDrManagerService;
import com.woniuxy.dr_mall.utils.Audience;
import com.woniuxy.dr_mall.utils.JwtUtil;
import com.woniuxy.dr_mall.valigroup.DrManagerAddGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@RestController
@CrossOrigin
@RequestMapping("/dr_mall/drManager")
public class DrManagerController {
    @Autowired
    private IDrManagerService managerService;
    @Autowired
    private IDrManagerMenuService drManagerMenuService;
    @Autowired
    private IDrManagerPermService drManagerPermService;
    
    @Autowired
    private Audience audience;

    @PostMapping("login")
    public MyResponseEntity<String> login(@RequestBody DrManager manager){
        DrManager newManager=null;
        try {
            newManager= managerService.getByAccount(manager.getAccount(),manager.getPassword());
            return new MyResponseEntity<>(JwtUtil.createJWT(newManager.getId(), newManager.getAccount(), audience));
        }catch (ServiceException e){
            if(e instanceof PasswordErrorException){
                return new MyResponseEntity<>(ResponseStateCode.PASSWORD_ERROR, "密码错误");
            }else if(e instanceof AccountNotExistException){
                return new MyResponseEntity<>(ResponseStateCode.PASSWORD_ERROR, "账户不存在");
            }
        }
        return null;
    }

    @GetMapping("getList")
    public MyResponseEntity<List<DrManager>> getDrManagers(){
        return new MyResponseEntity<>(managerService.getDrManagers());
    }
    @GetMapping("list")
    public MyResponseEntity<DrManager> getDrManagerById(Integer id){
        return new MyResponseEntity<>(managerService.getDrManagerById(id));
    }

    @PostMapping("add")
    public MyResponseEntity<DrManager> add(@RequestBody @Validated(DrManagerAddGroup.class)DrManager manager){

        return new MyResponseEntity<>(managerService.add(manager));
    }
    @PostMapping("update")
    public MyResponseEntity<?> update(@RequestBody @Validated(DrManagerAddGroup.class)DrManager manager){
        managerService.updateById(manager);
        QueryWrapper<DrManagerMenu> wrapper = new QueryWrapper<>();
        QueryWrapper<DrManagerPerm> wrapper2 = new QueryWrapper<>();
        wrapper.eq("managerId",manager.getId());
        wrapper2.eq("managerId",manager.getId());
        drManagerMenuService.remove(wrapper);
        drManagerPermService.remove(wrapper2);

        int managerId =  manager.getId();

        for (DrMenu menu : manager.getMenus()) {
            DrManagerMenu managerMenu = new DrManagerMenu();
            managerMenu.setManagerId(managerId);
            managerMenu.setMenuId(menu.getId());
            drManagerMenuService.save(managerMenu);
        }
        for (DrPerm perm : manager.getPerms()) {
            DrManagerPerm managerPerm = new DrManagerPerm();
            managerPerm.setManagerId(managerId);
            managerPerm.setPermId(perm.getId());
            drManagerPermService.save(managerPerm);
        }
        return  MyResponseEntity.SUCCESS;
    }
    @GetMapping("disable")
    public MyResponseEntity<?> getUnNormal(Integer id){
        managerService.updateUnNormalStatus(id);
        return MyResponseEntity.SUCCESS;
    }
    @GetMapping("enable")
    public MyResponseEntity<?> getNormal(Integer id){
        managerService.updateNormalStatus(id);
        return MyResponseEntity.SUCCESS;
    }
    @GetMapping("getPerms")
    public MyResponseEntity<List<DrPerm>> getDrManagerPerms(){
        return new MyResponseEntity<>(managerService.getPerms());
    }
    @GetMapping("getMenus")
    public MyResponseEntity<List<DrMenu>> getDrManagerMenus(){
        return new MyResponseEntity<>(managerService.getMenus());
    }
}

